package com.ifsaid.im.controller;

import com.ifsaid.im.bo.LoginUser;
import com.ifsaid.im.entity.UserEntity;
import com.ifsaid.im.exception.UserException;
import com.ifsaid.im.service.IUserService;
import com.ifsaid.im.vo.LoginUserVO;
import com.ifsaid.im.vo.RegisteredUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

/**
 * @description: 描述
 * @author: Wang Chen Chen<932560435@qq.com>
 * @date: 2019/11/4 16:02
 */

@Slf4j
@Controller
public class HomeController {

    private final static String LOGIN_USER = "login_user";

    @Autowired
    private IUserService userService;

    /**
     * @description: 首页
     * @date: 2019/11/4 16:03
     */
    @GetMapping(value = {"/", "/home", "/index"})
    public ModelAndView index(HttpSession session) {
        var loginUser = (LoginUser) session.getAttribute(LOGIN_USER);
        if (loginUser == null || StringUtils.isEmpty(loginUser.getUsername())) {
            return new ModelAndView("login", "loginUser", new LoginUserVO());
        }
        // 我的朋友列表
        Set<UserEntity> myFriends = userService.findFriendList(loginUser.getUid());
        return new ModelAndView("index", Map.of(LOGIN_USER, loginUser, "my_friends", myFriends));
    }

    /**
     * @description: 注册页面
     * @date: 2019/11/4 16:03
     */
    @GetMapping("/registered")
    public ModelAndView registered() {
        return new ModelAndView("registered", "registeredUser", new RegisteredUserVO());
    }

    /**
     * @description: 注册提交
     * @date: 2019/11/4 16:03
     */
    @PostMapping("/registered")
    public ModelAndView registered(@Validated RegisteredUserVO registeredUser,
                                   BindingResult br) {
        if (br.hasErrors()) {
            String errorMessage = br.getFieldError().getDefaultMessage();
            return new ModelAndView("errorMsg", "msg", errorMessage);
        }
        var target = new UserEntity();
        BeanUtils.copyProperties(registeredUser, target);
        try {
            userService.registered(target);
            return new ModelAndView("login");
        } catch (UserException ex) {
            return new ModelAndView("errorMsg", "msg", ex.getMessage());
        }
    }

    /**
     * @description: 登录页面
     * @date: 2019/11/4 16:03
     */
    @GetMapping("/login")
    public ModelAndView login() {
        return new ModelAndView("login", "loginUser", new LoginUserVO());
    }

    /**
     * @description: 登录提交
     * @date: 2019/11/4 16:03
     */
    @PostMapping("/login")
    public ModelAndView login(@Validated LoginUserVO loginUser, BindingResult br, HttpSession session) {
        if (br.hasErrors()) {
            String errorMessage = br.getFieldError().getDefaultMessage();
            return new ModelAndView("errorMsg", "msg", errorMessage);
        }
        try {
            LoginUser login = userService.login(loginUser.getUsername(), loginUser.getPassword());
            session.setAttribute(LOGIN_USER, login);
            return new ModelAndView("redirect:/index");
        } catch (UserException ex) {
            return new ModelAndView("errorMsg", "msg", ex.getMessage());
        }
    }


    /**
     * @description: 添加好友 页面
     * @date: 2019/11/4 16:03
     */
    @GetMapping("/addFriend")
    public ModelAndView addFriend() {
        return new ModelAndView("addFriend");
    }

    /**
     * @description: 添加好友 页面
     * @date: 2019/11/4 16:03
     */
    @PostMapping("/addFriend")
    public ModelAndView addFriend(String friendName, HttpSession session) {
        var loginUser = (LoginUser) session.getAttribute(LOGIN_USER);
        if (loginUser == null || StringUtils.isEmpty(loginUser.getUsername())) {
            return new ModelAndView("login", "loginUser", new LoginUserVO());
        }
        // 添加好友
        userService.addFriend(loginUser.getUid(), friendName);
        // 添加成功后，跳转到首页
        return new ModelAndView("redirect:/index");
    }

}
